package com.qfedu.dao.impl;

import com.qfedu.dao.GoodsTypeDao;
import com.qfedu.pojo.GoodsType;
import com.qfedu.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/4/4 9:40
 * 描述:
 */
public class GoodsTypeDaoImpl implements GoodsTypeDao {

    QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());

    @Override
    public List<GoodsType> getGoodsTypeLevel1() {
        String sql = "SELECT * FROM tb_goods_type where level = 1";
        try {
            List<GoodsType> goodsTypeList = qr.query(sql, new BeanListHandler<>(GoodsType.class));
            return goodsTypeList;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品分类失败");
        }
        return null;
    }

    @Override
    public GoodsType findById(int typeid) {
        String sql = "SELECT * FROM tb_goods_type where id = ?";
        try {
            GoodsType goodsType = qr.query(sql, new BeanHandler<>(GoodsType.class), typeid);
            return goodsType;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品分类失败");
        }
        return null;
    }
}
